package com.walter.epay.param;

import com.walter.epay.constant.EpayFieldConstant;
import com.walter.epay.param.request.EpayCreateOrderParam;
import com.walter.epay.param.response.EpayCallbackParam;
import com.walter.epay.paramenum.createorder.EpayCreateOrderTypeEnum;

import java.io.Serializable;
import java.util.Objects;

/**
 * 生成sign值参数
 *
 * @author wuchenxi
 * @date 2022-12-31 14:10:22
 */
public class EpaySignParam implements Serializable {

    /**
     * 商户订单号
     */
    private String payId;

    private EpayCreateOrderTypeEnum type;

    /**
     * 自定义参数
     */
    private String param;
    /**
     * 金额
     */
    private Double price;

    /**
     * 实际支付金额
     */
    private Double reallyPrice;

    /**
     * 通讯秘钥
     */
    private String secret;

    private Integer optType;

    private EpaySignParam(String payId, EpayCreateOrderTypeEnum type, String param, Double price, String secret, Integer optType) {
        this.payId = payId;
        this.type = type;
        this.param = param;
        this.price = price;
        this.secret = secret;
        this.optType = optType;
    }

    private EpaySignParam(String payId, EpayCreateOrderTypeEnum type, String param, Double price, Double reallyPrice, String secret, Integer optType) {
        this.payId = payId;
        this.type = type;
        this.param = param;
        this.price = price;
        this.secret = secret;
        this.reallyPrice = reallyPrice;
        this.optType = optType;
    }

    /**
     * 获取支付回调的signParam
     *
     * @param param  回调参数
     * @param secret 通讯秘钥
     * @return
     */
    public static EpaySignParam getCallbackSignParam(EpayCallbackParam param, String secret) {
        return new EpaySignParam(param.getOrderId(), EpayCreateOrderTypeEnum.getByCode(param.getType()), param.getParam(), param.getPrice(), param.getReallyPrice(), secret, EpayFieldConstant.SignParam.OptType.CALL_BACK);
    }

    /**
     * 获取创建订单时的signParam
     *
     * @param param  创建订单参数
     * @param secret 通讯秘钥
     * @return
     */
    public static EpaySignParam getCreateOrderSignStr(EpayCreateOrderParam param, String secret) {
        return new EpaySignParam(param.getPayId(), EpayCreateOrderTypeEnum.getByCode(param.getType()), param.getParam(), param.getPrice(), secret, EpayFieldConstant.SignParam.OptType.CREATE_ORDER);
    }

    public String getGenerateSignStr() {
        if (Objects.equals(this.optType, EpayFieldConstant.SignParam.OptType.CREATE_ORDER)) {
            return this.payId + this.param + this.type.getType() + this.price + this.secret;
        } else if (Objects.equals(this.optType, EpayFieldConstant.SignParam.OptType.CALL_BACK)) {
            return this.payId + this.param + this.type.getType() + this.price + this.reallyPrice + this.secret;
        }
        return null;
    }

    public Integer getOptType() {
        return optType;
    }

    public void setOptType(Integer optType) {
        this.optType = optType;
    }

    public void setReallyPrice(Double reallyPrice) {
        this.reallyPrice = reallyPrice;
    }

    public Double getReallyPrice() {
        return reallyPrice;
    }

    public String getPayId() {
        return payId;
    }

    public void setPayId(String payId) {
        this.payId = payId;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public EpayCreateOrderTypeEnum getType() {
        return type;
    }

    public void setType(EpayCreateOrderTypeEnum type) {
        this.type = type;
    }


}
